<template>
  <div class="app-container">
    <div style="width: 35%">
      <el-alert
        title="优惠券配置"
        type="success"
        show-icon
        :closable="false"
      ></el-alert>
      <div style="margin-top: 30px">
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="150px"
          class="demo-ruleForm"
        >
          <el-form-item label="优惠券过期时间 " prop="count">
            <el-input
              v-model="ruleForm.count"
              :min="1"
              type="number"
              style="width: 200px"
            ></el-input>
            <span style="margin-left: 10px">天</span>
          </el-form-item>
          <el-form-item label="优惠券满减金额比例" prop="ratio">
            <el-input
              v-model="ruleForm.ratio"
              :min="1"
              :max="100"
              type="number"
              style="width: 200px"
            ></el-input>
            <span style="margin-left: 10px">倍</span>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="mini" @click="handleQuery"
              >提交</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { getConfig, changeConfig } from "@/api/bonusPool/bonusPool";
import { Debounce } from "@/utils/tools.js";
export default {
  name: "productCate",
  dicts: ["user_level_list", "user_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      areaList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        areaName: "",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        count: [
          { required: true, message: "请输入优惠券过期时间", trigger: "blur" },
        ],
        ratio: [
          {
            required: true,
            message: "请输入优惠券满减金额比例",
            trigger: "blur",
          },
        ],
      },
      ruleForm: {
        count: null,
        ratio: null,
      },
      configInfo: {},
    };
  },
  created() {
    this.getConfigInfo();
  },
  methods: {
    // 获取配置
    getConfigInfo() {
      getConfig("coupon.expiration.time").then((res) => {
        if (res.code == 200) {
          this.ruleForm.count = Number(res.msg);
        }
      });
      getConfig("coupon.max.money").then((res) => {
        if (res.code == 200) {
          this.ruleForm.ratio = Number(res.msg);
        }
      });
    },
    /** 提交 */
    handleQuery() {
      let that = this;
      if (!that.ruleForm.ratio || !that.ruleForm.count) {
        that.$modal.msgError("请输入有效的配置信息");
        return;
      }
      that.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          let data1 = {
            configKey: "coupon.expiration.time",
            configValue: that.ruleForm.count,
          };
          changeConfig(data1)
            .then((res) => {
              if (res.code == 200) {
              }
            })
            .then(() => {
              let data2 = {
                configKey: "coupon.max.money",
                configValue: that.ruleForm.ratio,
              };
              changeConfig(data2).then((res) => {
                if (res.code == 200) {
                  that.$modal.msgSuccess("修改配置成功！");
                }
              });
            });
        }
      });
    },

    /** 查询列表 */
    getList() {
      this.loading = true;
      productAreaList(this.queryParams).then((response) => {
        this.areaList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品区域";
    },
    /** 修改按钮操作 */
    handleUpdate: Debounce(function (row) {
      this.reset();
      detailArea(row.areaId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品区域";
      });
    }, 500),
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 删除按钮操作 */
    handleDelete: Debounce(function (row) {
      this.$modal
        .confirm('是否确认删除名称为"' + row.areaName + '"的数据项？')
        .then(function () {
          return delArea(row.areaId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    }, 500),
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 提交按钮 */
    submitForm: Debounce(function (row) {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let data = {
            areaName: this.form.areaName,
          };
          if (this.form.areaId) {
            data.areaId = this.form.areaId;
            modifyArea(data).then((response) => {
              this.$modal.msgSuccess("编辑成功");
              this.open = false;
              this.getList();
            });
          } else {
            insertArea(data).then((response) => {
              this.$modal.msgSuccess("添加成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    }, 500),
  },
};
</script>
